Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(WIP): adding timestamp to exchange rates of denoms #2243

Closed
wants to merge 8 commits into from

Conversation

gsk967
Copy link
Collaborator

@gsk967 gsk967 commented Sep 13, 2023

Description

We are storing exchange prices with timestamps for denoms in Oracle on every block , so whenever any module requested GetExchangeRate from oracle it will return latest exchange rate of denom from store.

It will keep no of HistoricStampPeriod records for every denom on oracle , so whenever chain halted or no prices from price-feeder , oracle will return latest exchange rate of denom from store.

Messages and Genesis ---

message Genesis {
	...
  repeated ExchangeRatesWithTimestamp exchange_rates_timestamps = 11 [
    (gogoproto.nullable) = false
  ];
}

// ExchangeRatesWithTimestamp - store the exchange rate of denom with price timestamp
message ExchangeRatesWithTimestamp {
  ExchangeRateTuple exchange_rate_tuples = 1 [
    (gogoproto.moretags)     = "yaml:\"exchange_rate_tuples\"",
    (gogoproto.castrepeated) = "ExchangeRateTuples",
    (gogoproto.nullable)     = false
  ];
  // Unix timestamp when the first price was aggregated in the counter
  google.protobuf.Timestamp timestamp = 2 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
}

// ExchangeRateTuple - struct to store interpreted exchange rates data to store
message ExchangeRateTuple {
  option (gogoproto.equal)            = false;
  option (gogoproto.goproto_getters)  = false;
  option (gogoproto.goproto_stringer) = false;

  string denom         = 1 [(gogoproto.moretags) = "yaml:\"denom\""];
  string exchange_rate = 2 [
    (gogoproto.moretags)   = "yaml:\"exchange_rate\"",
    (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
    (gogoproto.nullable)   = false
  ];
}

Query ---

service Query {
	...
	
  // ExgRatesWithTimestamps returns exchange prices of denoms with timestamps.
  rpc ExgRatesWithTimestamps(QueryExgRatesWithTimestamps)
      returns (QueryExgRatesWithTimestampsResponse) {
    option (google.api.http).get =
        "/umee/historacle/v1/exg_rates";
  }
}


// QueryExgRateWithTimestamps is the request type for the Query/ExgRatesWithTimestamps RPC
// method.
message QueryExgRatesWithTimestamps {
  // denom defines the denomination to query for.
  string denom = 1;
}

// QueryHistoricPricesResponse is response type for the
// Query/ExgRatesWithTimestamps RPC method.
message QueryExgRatesWithTimestampsResponse {
  repeated ExchangeRatesWithTimestamp exg_rates = 1;
}

Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • added appropriate labels to the PR
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

Comment on lines +204 to +215
for _, v := range exgRatesForDenom {
if len(v) > int(noOfRecords) {
ers := v
// sort the list with descending order by timestamp
// only keep latest noOfRecords
sort.Slice(ers, func(i, j int) bool { return ers[i].Timestamp.After(ers[j].Timestamp) })
// exgRatesForDenom[k] = ers
for _, d := range ers[noOfRecords:] {
k.DeleteExgRateWithTimestamp(ctx, d.ExchangeRateTuples.Denom, d.Timestamp)
}
}
}

Check warning

Code scanning / CodeQL

Iteration over map

Iteration over map may be a possible source of non-determinism
@gsk967 gsk967 marked this pull request as ready for review September 14, 2023 10:27
@gsk967 gsk967 requested review from a team as code owners September 14, 2023 10:27
@codecov
Copy link

codecov bot commented Sep 14, 2023

Codecov Report

Merging #2243 (213d48a) into main (7f05ad4) will decrease coverage by 5.41%.
Report is 230 commits behind head on main.
The diff coverage is 84.48%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2243      +/-   ##
==========================================
- Coverage   75.38%   69.98%   -5.41%     
==========================================
  Files         100      168      +68     
  Lines        8025    12748    +4723     
==========================================
+ Hits         6050     8922    +2872     
- Misses       1589     3226    +1637     
- Partials      386      600     +214     
Files Changed Coverage Δ
ante/spam_prevention.go 75.92% <ø> (ø)
cmd/ibc_denom/main.go 0.00% <0.00%> (ø)
util/coin/coin.go 12.50% <0.00%> (ø)
util/sdkutil/msg.go 0.00% <0.00%> (ø)
x/incentive/codec.go 47.82% <ø> (+9.89%) ⬆️
x/incentive/keeper/genesis.go 40.90% <ø> (ø)
x/incentive/keeper/grpc_query.go 59.18% <ø> (ø)
x/incentive/keeper/hooks.go 50.00% <ø> (ø)
x/incentive/keeper/invariants.go 0.00% <ø> (ø)
x/incentive/keeper/iter.go 84.90% <ø> (ø)
... and 51 more

... and 85 files with indirect coverage changes

@@ -118,6 +118,17 @@ message AggregateExchangeRateVote {
string voter = 2 [(gogoproto.moretags) = "yaml:\"voter\""];
}

// ExchangeRatesWithTimestamp - store the exchange rate of denom with price timestamp
message ExchangeRatesWithTimestamp {
ExchangeRateTuple exchange_rate_tuples = 1 [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this repeated or single?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

single only

Copy link
Member

@toteki toteki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make sure to clarify the design here.

We need to allow:

  • Scenarios where most denoms have up to date prices, but one or two tokens have old prices
  • Leverage module must be able to know the age of the price it received (and this information must vary per denom)
  • Leverage module will need PriceModeRecent which allows old prices for frontend, but all other price modes like PriceModeSpot reject old prices. I can build this when the time comes

@robert-zaremba
Copy link
Member

@gsk967 , @toteki I had a bit different implementation in my mind. I left few todos in the code to follow up: #2249

@gsk967
Copy link
Collaborator Author

gsk967 commented Sep 27, 2023

I am closing this pull request

@gsk967 gsk967 closed this Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants